<?php

namespace App\Imports;

use App\Models\FsUser;
use App\Models\FsUserTask;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\WithBatchInserts;

class FsUserImport implements ToModel, WithChunkReading, WithBatchInserts
{
    protected $user_id;
    public function __construct($user_id = '')
    {
        $this->user_id = $user_id;
    }

    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        $res = FsUser::where('username',$row[0])->first();
        //会员ID存在
        if($res){
            $result = FsUserTask::where([
                ['fs_user_id','=', $res->id],
                ['carry_user_id','=',$this->user_id]
            ])->first();
            //任务关联表中没有记录的时候才插入
            if(!$result){
                return new FsUserTask([
                    'task_id' => null,
                    'fs_user_id' => $res->id,
                    'carry_user_id' => $this->user_id
                ]);
            }
        }
    }
    public function batchSize(): int
    {
        return 1000;
    }

    public function chunkSize(): int
    {
        return 1000;
    }
}
